<!DOCTYPE html>
<html>
	<head>
		<title>商品分类表-添加</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<!-- 所有的 css js 资源 -->
		<link rel="stylesheet" href="https://unpkg.com/element-ui@2.13.0/lib/theme-chalk/index.css">
		<link rel="stylesheet" href="../../static/sa.css"> 
		<script src="https://unpkg.com/vue@2.6.10/dist/vue.js"></script>
		<script src="https://unpkg.com/http-vue-loader@1.4.2/src/httpVueLoader.js"></script>
		<script src="https://unpkg.com/element-ui@2.13.0/lib/index.js"></script>
		<script src="https://unpkg.com/jquery@3.4.1/dist/jquery.js"></script>
		<script src="https://www.layuicdn.com/layer-v3.1.1/layer.js"></script>
		<script src="../../static/sa.js"></script>
		<style type="text/css">
			.c-panel .el-form .c-label{width: 6em !important;}
			.c-panel .el-form .el-input{width: 250px;}
		</style>
	</head>
	<body>
		<div class="vue-box" style="display: block;">
			<!-- ------- 内容部分 ------- -->
			<div class="s-body">
				<div class="c-panel">
					<div class="c-title">分类添加</div>
					<el-form v-if="m">
						<sa-item name="分类名字" v-model="m.name" br></sa-item>
						<!-- <sa-item name="分类图标" v-model="m.icon" br></sa-item>
						<sa-item name="排序索引" v-model="m.sort" br type="num"></sa-item> -->
						<sa-item name="" br class="s-ok">
							<el-button type="primary" icon="el-icon-plus" @click="ok()">保存</el-button>
						</sa-item>
					</el-form>
				</div>
			</div>
			<!-- ------- 底部按钮 ------- -->
			<div class="s-foot">
				<el-button type="primary" @click="ok()">确定</el-button>
				<el-button @click="sa.closeCurrIframe()">取消</el-button>
			</div>
		</div>
		<script src="./moke-data.js"></script>
        <script>
			var app = new Vue({
				components: {
					"sa-item": httpVueLoader('../../sa-frame/com/sa-item.vue'),		
				},
				el: '.vue-box',
				data: {
					m: null,		// 实体对象 
				},
				methods: {
					async addCategory() {
						let res = await $.ajax({
							url: 'http://localhost:8080/category/addCategory',
							type: 'POST',
							contentType: 'application/json',
							data: JSON.stringify(
								{ name: this.m.name }
							),
							success: function(res) {
								console.log(res);
							},
							error: function(err) {
								console.log(err);
							}
						});
					},
					async updateCategory() {
						let res = await $.ajax({
							url: 'http://localhost:8080/category/updateCategory',
							type: 'POST',
							contentType: 'application/json',
							data: JSON.stringify(
								{ id: this.id, name: this.m.name }
							),
							success: function(res) {
								console.log(res);
							},
							error: function(err) {
								console.log(err);
							}
						});
					},
					// 创建一个 默认Model 
					createModel: function() {
						return {
							name: '',		// 分类名字  
							// icon: '',		// 分类图标  
							// sort: '',		// 排序值  
							// create_time: '',		// 创建日期  
						}
					},
					// 提交数据 
					ok: function(){
						// 表单校验  
						sa.checkNull(this.m.name, '请输入分类名字');
						// 添加
						$.ajax({
							url: 'http://localhost:8080/category/addCategory',
							type: 'POST',
							contentType: 'application/json',
							data: JSON.stringify(this.m),
							success: function(res){
								if(res.code == 200){
									sa.alert('添加成功', this.clean); 
									sa.closeCurrIframe();
									parent.app.f5();  // 刷新父页面列表
								}else{
									sa.alert(res.msg);
								}
							},
							error: function(res){
								sa.alert('添加失败');
							}
						});
						// sa.checkNull(this.m.icon, '请输入分类图标');
						// sa.checkNull(this.m.sort, '请输入排序值');
						// 开始增加或修改
						this.m.create_time = undefined;		// 不提交属性：创建日期
						if(this.id <= 0) {	// 添加
							this.addCategory();
							sa.alert('增加成功', this.clean);
						} else {	// 修改
							this.updateCategory();
							sa.alert('修改成功', this.clean);
						}
					},
					// 添加完成后的动作
					clean: function() {
						this.m = this.createModel();
					}
				},
				mounted: function(){
					// 初始化数据 
					this.m = this.createModel();
				}
			})
		</script>
	</body>
</html>